package com.yanglanting.disk.user.control;

import com.alibaba.fastjson.JSON;
import com.yanglanting.disk.mybatis.entity.PtUserEntity;
import com.yanglanting.disk.mybatis.flexigrid.PagingEntity;
import com.yanglanting.disk.mybatis.flexigrid.TblResponseEntity;
import com.yanglanting.disk.mybatis.ztree.ZtreeEntity;
import com.yanglanting.disk.spring.control.BaseController;
import com.yanglanting.disk.user.entity.SearchUserEntity;
import com.yanglanting.disk.user.exeption.EmailRepeatException;
import com.yanglanting.disk.user.exeption.MobileRepeatException;
import com.yanglanting.disk.user.exeption.UserNameRepeatException;
import com.yanglanting.disk.user.exeption.UserOccupyException;
import com.yanglanting.disk.user.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    @Resource
    private UserService userService;

    @RequestMapping("/queryUserList")
    @ResponseBody
    public List<PtUserEntity> queryUserList(){
        List<PtUserEntity> list = userService.queryUserList();
        return  list;
    }

    @Secured("ROLE_cldk_data_user_del")
    @RequestMapping("/deleteUserByIds")
    @ResponseBody
    public String deleteUserByIds(@RequestParam("ids") String ids){
        try {
            userService.deleteUserByIds(ids);
        } catch (UserOccupyException e) {
//            e.printStackTrace();
            return failJson("user.delete.fail",e.getMessage());
        }
        return successJson("user.delete.success");

    }
    @Secured("ROLE_cldk_data_user")
    @RequestMapping("/userPage")
    public String userPage(){
        return "user/userPage";
    }

    @RequestMapping("/queryUserInfoListByPage")
    @ResponseBody
    public TblResponseEntity<PtUserEntity> queryUserInfoListByPage(SearchUserEntity entity,PagingEntity paging){
        TblResponseEntity<PtUserEntity> responseEntity = userService.queryUserInfoListByPage(entity,paging);
        return responseEntity;
    }

    @Secured("ROLE_cldk_data_user")
    @RequestMapping("/queryUserInfoById")
    @ResponseBody
    public String queryUserInfoById(@RequestParam("userUuid") String userUuid){
        PtUserEntity entity = userService.queryUserInfoById(userUuid);
        return JSON.toJSONString(entity);
    }
    @Secured("ROLE_cldk_data_user_mod")
    @RequestMapping("/modifyUserInfoById")
    @ResponseBody
    public String modifyUserInfoById(PtUserEntity entity){
        try {
            userService.modifyUserInfoById(entity);
        } catch (EmailRepeatException e) {
            return failJson("user.email.repeat");
//            e.printStackTrace();
        } catch (MobileRepeatException e) {
            return failJson("user.mobile.repeat");
//            e.printStackTrace();
        } catch (UserNameRepeatException e) {
            return failJson("user.name.repeat");
//            e.printStackTrace();
        }
        return successJson("user.modify.success");
    }
    @Secured("ROLE_cldk_data_user_add")
    @RequestMapping("/addUserInfo")
    @ResponseBody
    public String addUserInfo(PtUserEntity entity){
        try {
            userService.addUserInfo(entity);
        } catch (EmailRepeatException e) {
            return failJson("user.email.repeat");
//            e.printStackTrace();
        } catch (MobileRepeatException e) {
            return failJson("user.mobile.repeat");
//            e.printStackTrace();
        } catch (UserNameRepeatException e) {
            return failJson("user.name.repeat");
//            e.printStackTrace();
        }
        return successJson("user.add.success");
    }
    @Secured("ROLE_cldk_data_user_role")
    @RequestMapping("/queryRoleTreeListByUser")
    @ResponseBody
    public String queryRoleTreeListByUser(@Param("userUuid") String userUuid){
        List<ZtreeEntity> list = userService.queryRoleTreeListByUser(userUuid);
        return JSON.toJSONString(list);
    }
    @Secured("ROLE_cldk_data_user_role")
    @RequestMapping("/saveRoleRefUser")
    @ResponseBody
    public String saveRoleRefUser(@Param("userUuidArray") String userUuidArray,@Param("roleUuidArray") String roleUuidArray){
        userService.saveRoleRefUser(userUuidArray,roleUuidArray);
        return successJson("user.allocat.role.success");
    }






}
